﻿
using IService;
using Microsoft.AspNetCore.Mvc;
using ModelDto;
using Newtonsoft.Json;
using System.Collections.Generic;
using System;
using System.Linq;
using Model.Models;
using Common;
using AssetManegement.Filter;
using Model.Models.Auto;
using Service;

namespace Api.Controllers
{
    /// <summary>
    /// Job接口
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [ServiceFilter(typeof(MyTokenFilterAttribute))]
    public class JobController : ControllerBase
    {
        private readonly IJobService _jobService;

        public JobController(IJobService jobService)
        {
            _jobService = jobService;
        }




        ///// <summary>
        ///// 获取Job列表
        ///// </summary>
        ///// <returns></returns>
        //[HttpPost]
        //public JsonResultL GetJobList(JobRequestPage request)
        //{
        //    JsonResultL response = new JsonResultL();

        //    string tempUser = HttpContext.Request.Headers["LUserInfo"].ToString();
        //    AccountLoginInfo CurrentUser = JsonConvert.DeserializeObject<AccountLoginInfo>(tempUser);

        //    if (CurrentUser == null || CurrentUser.Account == null)
        //    {
        //        return response.TokenFail("用户异常");
        //    }

        //    List<JobResponse> list = new List<JobResponse>();


        //    _jobService.QueryList()

        //    //数据少的读缓存一次性取出后再分页 数据大的去数据库分页取数据
        //    List<Job> resList = null;
        //    #region 读redis缓存

        //    resList = _jobService.GetListCache();

        //    //关键字条件筛选
        //    //if (!string.IsNullOrWhiteSpace(request.KeyWords))
        //    //{
        //    //    resList = resList.Where(a => a.Col1.Contains(request.KeyWords)).ToList();
        //    //}

        //    int count = resList.Count();
        //    if (request.PageIndex <= 1)
        //    {
        //        request.PageIndex = 1;
        //    }
        //    if (request.PageSize < 1)
        //    {
        //        request.PageSize = 10;
        //    }

        //    resList = resList.OrderBy(a => a.CreateTime).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();

        //    #endregion

        //    #region 读数据库

        //    //resList = _jobService.QueryList(a => true, request.PageIndex, request.PageSize, out count, b => b.CreateTime, true);

        //    #endregion

        //    if (resList != null && resList.Count > 0)
        //    {
        //        foreach (var item in resList)
        //        {
        //            var temp = _entityMapperHelper.Mapper<JobResponse, Job>(item);

        //            list.Add(temp);
        //        }
        //    }
        //    return response.Sucess(new { list = list, total = count });
        //}

        ///// <summary>
        ///// Job - 新增 修改
        ///// </summary>
        ///// <param name="request"></param>
        ///// <returns></returns>
        //[HttpPost]
        //public JsonResultL SaveJob(JobRequest request)
        //{
        //    JsonResultL response = new JsonResultL();
        //    DateTime now = DateTime.Now;
        //    string tempUser = HttpContext.Request.Headers["LUserInfo"].ToString();
        //    AccountLoginInfo CurrentUser = JsonConvert.DeserializeObject<AccountLoginInfo>(tempUser);

        //    if (CurrentUser == null || CurrentUser.Account == null)
        //    {
        //        return response.TokenFail("用户异常");
        //    }

        //    var entity = new Job();

        //    if (string.IsNullOrWhiteSpace(request.Id))
        //    {
        //        //新增

        //        //过滤条件
        //        //var entity = _jobService.Query(a => a.Name == request.Name);
        //        //if (entity != null)
        //        //{
        //        //    return response.Fail("名称已存在");
        //        //}



        //        entity.Remark = request.Remark;
        //        entity.CreateTime = now;
        //        entity.CreateUser = CurrentUser.Account.Id;

        //        entity.Name = request.Name;


        //        bool flag = _jobService.Add(entity);

        //        return flag ? response.Sucess("新增成功") : response.Fail("新增失败");
        //    }
        //    else
        //    {
        //        //修改

        //        //过滤条件
        //        //entity = _jobService.Query(a => a.Name == request.Name && a.Id != request.Id);
        //        //if (entity != null)
        //        //{
        //        //    return response.Fail("名称已存在");
        //        //}

        //        entity = _jobService.Find(request.Id);
        //        if (entity == null)
        //        {
        //            return response.Fail("数据异常");
        //        }


        //        entity.Remark = request.Remark;
        //        entity.UpdateUser = CurrentUser.Account.Id;
        //        entity.UpdateTime = now;

        //        entity.Name = request.Name;

        //        bool flag = _jobService.Update(entity);

        //        return flag ? response.Sucess("修改成功") : response.Fail("修改失败");
        //    }


        //}

        ///// <summary>
        ///// Job-删除
        ///// </summary>
        ///// <param name="request"></param>
        ///// <returns></returns>
        //[HttpPost]
        //public JsonResultL DeleteJob(DeleteRequest request)
        //{
        //    JsonResultL response = new JsonResultL();
        //    DateTime now = DateTime.Now;
        //    string tempUser = HttpContext.Request.Headers["LUserInfo"].ToString();
        //    AccountLoginInfo CurrentUser = JsonConvert.DeserializeObject<AccountLoginInfo>(tempUser);

        //    if (CurrentUser == null || CurrentUser.Account == null)
        //    {
        //        return response.TokenFail("用户异常");
        //    }
        //    if (string.IsNullOrWhiteSpace(request.Id))
        //    {
        //        return response.Fail("参数错误");
        //    }


        //    var entity = _jobService.Find(request.Id);
        //    if (entity == null)
        //    {
        //        return response.Fail("数据异常");
        //    }

        //    //软删除
        //    entity.IsDelete = true;
        //    entity.DeleteUser = CurrentUser.Account.Id;
        //    entity.DeleteTime = now;

        //    bool flag = _jobService.Update(entity);


        //    return flag ? response.Sucess("删除成功") : response.Fail("删除失败");
        //}
    }
}

